package lumis.portal.file;

import lumis.portal.PortalException;
import lumis.portal.PortalObjectAlreadyExistsException;
import lumis.portal.PortalObjectNotFoundException;
import lumis.portal.UnexpectedException;
import lumis.portal.dao.DaoException;
import lumis.portal.dao.jdbc.ITransactionJdbc;
import lumis.util.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Stores files information
 *
 * @version $Revision: 7409 $ $Date: 2007-06-13 18:37:18 -0300 (Wed, 13 Jun 2007) $
 * @since 4.0.0
 */
public class FilesDaoJdbc implements IFilesDao
{
	public FilesConfig get(String filesId, ITransaction transaction) throws PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("select filesId from lum_Files where filesId = ?");
			try
			{
				statement.setString(1, filesId);
				ResultSet resultSet = statement.executeQuery();
				try
				{
					if (resultSet.next())
					{
						FilesConfig filesConfig = new FilesConfig();
						filesConfig.setId(filesId);
						return filesConfig;
					}
					else
						return null;

				}
				finally
				{
					resultSet.close();
				}
			}
			finally
			{
				statement.close();
			}
		}
		catch (SQLException e)
		{
			throw new UnexpectedException(e);
		}
	}

	public void add(FilesConfig filesConfig, ITransaction transaction) throws PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("insert into lum_Files " +
					"(filesId) " +
					"values (?)");
			try
			{
				statement.setString(1, filesConfig.getId());
				try
				{
					statement.executeUpdate();
				}
				catch (Exception e)
				{
					try
					{
						if(get(filesConfig.getId(), transaction) != null)
							throw new PortalObjectAlreadyExistsException("STR_RESOURCE_ALREADY_EXISTS;" + filesConfig.getId(), e);
					}
					catch (PortalObjectNotFoundException e1)
					{
						throw e;
					}
				}
			}
			finally
			{
				statement.close();
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}

	public void delete(String filesId, ITransaction transaction) throws PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("delete from lum_Files where filesId = ?");
			try
			{
				statement.setString(1, filesId);
				statement.executeUpdate();
			}
			finally
			{
				statement.close();
			}
		}
		catch (SQLException e)
		{
			throw new UnexpectedException(e);
		}
	}

	public void update(FilesConfig filesConfig, ITransaction transaction) throws DaoException, PortalException
	{
		/*
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("update lum_Files set filesId = ?, where filesId = ?");
			try
			{
				statement.setString(1, filesConfig.getId());
				statement.setString(2, filesConfig.getId());
				statement.executeUpdate();
			}
			finally
			{
				statement.close();
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
		*/
	}

	public void deleteFilesLists(String filesId, ITransaction transaction) throws PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("delete from lum_FilesList where filesId = ?");
			try
			{
				statement.setString(1, filesId);
				statement.executeUpdate();
			}
			finally
			{
				statement.close();
			}
		}
		catch (SQLException e)
		{
			throw new UnexpectedException(e);
		}
	}
}
